package loes0;

public class DiscreteTimeCounter {

	private long[] arrivalTimes;
	private long[] initTimes;
	private long[] completionTimes;

	public DiscreteTimeCounter(Stichproben stichproben) {
		super();
		Customer[] customers = stichproben.sortableQueueToArray();
		int length = customers.length;
		arrivalTimes = new long[length];
		initTimes = new long[length];
		completionTimes = new long[length];
		calcTimes(customers);
	}

	private void calcTimes(Customer[] customers) {
		for (int i = 0; i < customers.length; i++) {
			arrivalTimes[i] = customers[i].getTimeOfArrival();
			initTimes[i] = customers[i].getServiceInitiation();
			completionTimes[i] = customers[i].getServiceCompletion();
		}

	}

	// 2.28
	public long generalEmpiricalMoment(long[] times) {
		return sumFactor(times);
	}

	public long generalEmpiricalMomentOrdering(int order, long[] times) {
		// erwartungswert
		if (order == 1) {
			return generalEmpiricalMoment(times);
		}
		// varianz
		if (order == 2) {
			long[] quadArray = quadLongArray(times);
			long ex_12 = generalEmpiricalMoment(quadArray);
			long ex_2 = generalEmpiricalMoment(times);
			long ex_22 = ex_2 * ex_2;
			return  ex_12 -(ex_22);
		}
		return 0;

	}

	// eine summe berechnen
	private long sumFactor(long[] stichproben) {
		int acc = 0;
		double factor = (double) 1 / stichproben.length;
		for (int i = 0; i < stichproben.length; i++) {
			acc += factor * stichproben[i];
		}
		return acc;
	}

	private long[] quadLongArray(long[] times) {
		long acc = 0;
		long acc2[] = new long[times.length];
		for (int i = 0; i < times.length; i++) {
			acc = times[i];
			acc2[i] = acc * acc;
//			acc += (generalEmpiricalMoment(quadLongArray(times))-(generalEmpiricalMoment(times) * generalEmpiricalMoment(times)));
		}
		
		return acc2;

	}
	
	public void report(){
		System.out.println("\nOrder 1: \n" +
				"Arrival Time: " + generalEmpiricalMomentOrdering(1, arrivalTimes) + " \n" +
				"Service Initiation Time: " + generalEmpiricalMomentOrdering(1, initTimes) + " \n" +
				"Service Completion Time: " + generalEmpiricalMomentOrdering(1, completionTimes) + " \n\n" +
				"Order 2: \n" +
				"Arrival Time: " + generalEmpiricalMomentOrdering(2, arrivalTimes) + " \n" +
				"Service Initiation Time: " + generalEmpiricalMomentOrdering(2, initTimes) + " \n" +
				"Service Completion Time: " + generalEmpiricalMomentOrdering(2, completionTimes) + " \n\n"						
				);
	}
}
